//
//  DateTool.m
//  分类总结-字符串和时间的故事
//
//  Created by tianlong on 2018/3/12.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "DateTool.h"

@implementation DateTool

#pragma mark - secs to date
+ (NSDate *)toDateFromSecsString:(NSString *)secsStr {
    NSInteger num = [secsStr integerValue];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:num];
    return date;
}

#pragma mark - secs to string
+ (NSString *)toTimeStringFromSecsString:(NSString *)secsStr dateFormatString:(NSString *)formatStr {
    NSInteger num = [secsStr integerValue];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:num];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = formatStr;
    NSString *timeStr = [formatter stringFromDate:date];
    return timeStr;
}

+ (NSString *)toTimeStringFromSecsString:(NSString *)secsStr dateFormat:(NSDateFormatter *)dateFormat {
    NSInteger num = [secsStr integerValue];
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:num];
    NSString *timeStr = [dateFormat stringFromDate:date];
    return timeStr;
}

#pragma mark - date to string，如：NSDate转2018:09:09
+ (NSString *)toTimeStringFromDate:(NSDate *)date dateFormatString:(NSString *)formatStr {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = formatStr;
    NSString *timeStr = [formatter stringFromDate:date];
    return timeStr;
}

+ (NSString *)toTimeStringFromDate:(NSDate *)date dateFormat:(NSDateFormatter *)dateFormat {
    NSString *timeString = [dateFormat stringFromDate:date];
    return timeString;
}

#pragma mark - string to date，如：2018:09:09转NSDate
/**
 string转date要求比较严格，format的格式必须一一对
 例如时间为 2018-09-09，则format必须为 yyyy-MM-dd
 */
+ (NSDate *)toDateFromTimeString:(NSString *)timeStr dateFormatString:(NSString *)formatStr {
    NSDate *date = nil;
    if (formatStr) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = formatStr;
        date = [formatter dateFromString:timeStr];
    }
    return date;
}

+ (NSDate *)toDateFromTimeString:(NSString *)timeStr dateFormat:(NSDateFormatter *)dateFormat {
    NSDate *date = nil;
    if (dateFormat) {
        date = [dateFormat dateFromString:timeStr];
    }
    return date;
}

@end
